package sk.tuke.android4all.services;

import android.content.Context;
import android.content.ServiceConnection;
import android.media.AudioManager;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.util.Log;

import java.util.HashMap;
import java.util.Locale;

/**
 * Created by Ing. Tibor Fecko on 26.2.2014.
 */
public class Tts {

	private static String TAG = "Tts";
    private Context mContext;
    private TextToSpeech aTtsEngine;
    private boolean isCompleted = true;

    @SuppressWarnings("deprecation")
	public Tts(Context aContext_in){
        mContext = aContext_in;
        aTtsEngine = new TextToSpeech(aContext_in, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int initStatus) {
                if (initStatus == TextToSpeech.SUCCESS) {
                    aTtsEngine.setLanguage(Locale.US);
                }
            }
        });
        aTtsEngine.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener(){

			@Override
			public void onUtteranceCompleted(String arg0) {
				Log.i(TAG, "Speech completed");
				isCompleted = true;
			}});
    }

    public void sayMessage(String aMessage_in){
    	isCompleted = false;
    	HashMap<String, String> mAlarms = new HashMap<String, String>();
        mAlarms.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));
        mAlarms.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "ENDED");
        aTtsEngine.speak(aMessage_in, TextToSpeech.QUEUE_FLUSH, mAlarms);
    }
       
    public boolean isSpeechCompleted(){
    	return isCompleted;
    }

    public void dispose(){
        aTtsEngine.shutdown(); 
    }
}

